﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace Client
{
    class USBDetect
    {
        public event USBDetectedEventHandler OnUSBDetected = null;
        private Thread startUSBDetect = null;

        public void Start()
        {
            this.startUSBDetect = new Thread(new ThreadStart(StartUSBDetect));
            this.startUSBDetect.Start();
        }

        private void StartUSBDetect()
        {
            while (true)
            {
                Thread.Sleep(1000);
                foreach (DriveInfo d in DriveInfo.GetDrives())
                {
                    if (d.DriveType == DriveType.Removable)
                    {
                        if (this.OnUSBDetected != null)
                        {
                            this.OnUSBDetected(this, new USBDetectedEventArgs());
                        }
                        this.Stop();
                        break;
                    }
                }
            }
        }

        public void Stop()
        {
            if (this.startUSBDetect != null)
            {
                this.startUSBDetect.Abort();
            }
        }
    }
}
